<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>std.typecons</title>
  <link href="./css/style.css" rel="stylesheet" type="text/css"/>
  <!-- <link href="./img/icon.png" rel="icon" type="image/png"/> -->
  <script type="text/javascript" src="./js/jquery.js"></script>
  <script type="text/javascript" src="./js/modules.js"></script>
  <script type="text/javascript" src="./js/quicksearch.js"></script>
  <script type="text/javascript" src="./js/navigation.js"></script>
  <!--<script type="text/javascript" src="./js/jquery.treeview.js"></script>-->
  <script type="text/javascript">
    var g_moduleFQN = "std.typecons";
  </script>
  
</head>
<body>
<div id="content">
  <h1><a href="./htmlsrc/std.typecons.html" class="symbol">std.typecons</a></h1>
  
<div class="summary">This module implements a variety of type constructors, i.e., templates
that allow construction of new, useful general-purpose types.</div>
<p class="sec_header">Synopsis:</p><pre class="d_code">
<span class="lc">// value tuples</span>
<span class="k">alias</span> <span class="i">Tuple</span>!(<span class="k">float</span>, <span class="sl">"x"</span>, <span class="k">float</span>, <span class="sl">"y"</span>, <span class="k">float</span>, <span class="sl">"z"</span>) <span class="i">Coord</span>;
<span class="i">Coord</span> <span class="i">c</span>;
<span class="i">c</span>.<span class="i">_0</span> = <span class="n">1</span>;         <span class="lc">// access by index-based name</span>
<span class="i">c</span>.<span class="i">field</span>!(<span class="n">1</span>) = <span class="n">1</span>;  <span class="lc">// access by index</span>
<span class="i">c</span>.<span class="i">z</span> = <span class="n">1</span>;          <span class="lc">// access by given name</span>
<span class="k">alias</span> <span class="i">Tuple</span>!(<span class="i">string</span>, <span class="i">string</span>) <span class="i">DicEntry</span>; <span class="lc">// names can be omitted</span>

<span class="lc">// enumerated values with conversions to and from strings</span>
<span class="k">mixin</span>(<span class="i">defineEnum</span>!(<span class="sl">"Openmode"</span>, <span class="sl">"READ"</span>, <span class="sl">"WRITE"</span>, <span class="sl">"READWRITE"</span>, <span class="sl">"APPEND"</span>));
<span class="k">void</span> <span class="i">foo</span>()
{
    <span class="i">Openmode</span> <span class="i">m</span> = <span class="i">Openmode</span>.<span class="i">READ</span>;
    <span class="i">string</span> <span class="i">s</span> = <span class="i">enumToString</span>(<span class="i">m</span>);
    <span class="k">assert</span>(<span class="i">s</span> == <span class="sl">"READ"</span>);
    <span class="i">Openmode</span> <span class="i">m1</span>;
    <span class="k">assert</span>(<span class="i">enumFromString</span>(<span class="i">s</span>, <span class="i">m1</span>) &amp;&amp; <span class="i">m1</span> == <span class="i">m</span>);
}

<span class="lc">// Rebindable references to const and invariant objects</span>
<span class="k">void</span> <span class="i">bar</span>()
{
    <span class="k">const</span> <span class="i">w1</span> = <span class="k">new</span> <span class="i">Widget</span>, <span class="i">w2</span> = <span class="k">new</span> <span class="i">Widget</span>;
    <span class="i">w1</span>.<span class="i">foo</span>(); 
    <span class="lc">// w1 = w2 would not work; can't rebind const object</span>
    <span class="k">auto</span> <span class="i">r</span> = <span class="i">Rebindable</span>!(<span class="k">const</span> <span class="i">Widget</span>)(<span class="i">w1</span>);
    <span class="lc">// invoke method as if r were a Widget object</span>
    <span class="i">r</span>.<span class="i">foo</span>(); 
    <span class="lc">// rebind r to refer to another object</span>
    <span class="i">r</span> = <span class="i">w2</span>;
}
</pre>
<p class="sec_header">Author:</p><a href="http://erdani.org">Andrei Alexandrescu</a>, Bartosz Milewski
<dl>
<dt class="decl">struct <a class="symbol _struct" name="Unique" href="./htmlsrc/std.typecons.html#L93" kind="struct" beg="93" end="195">Unique</a><span class="tparams">(T)</span>; <a title="Permalink to this symbol" href="#Unique" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L93">#</a></dt>
<dd class="ddef">
<div class="summary">Encapsulates unique ownership of a resource. 
Resource of type T is deleted at the end of the scope, unless it is transferred.
The transfer can be explicit, by calling <span class="d_psymbol"><i>release</i></span>, or implicit, when returning
Unique from a function. The resource can be a polymorphic class object, in which case
Unique behaves polymorphically too.</div>
<p class="sec_header">Example:</p>
<dl>
<dt class="decl">alias T <a class="symbol _alias" name="Unique.RefT" href="./htmlsrc/std.typecons.html#L96" kind="alias" beg="96" end="96">RefT</a>; <a title="Permalink to this symbol" href="#Unique.RefT" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L96">#</a></dt>
<dd class="ddef"></dd>
<dt class="decl"><a class="symbol _ctor" name="Unique.this" href="./htmlsrc/std.typecons.html#L121" kind="ctor" beg="121" end="125">this</a><span class="params">(RefT <em>p</em>)</span>; <span class="attrs">[<span class="prot">public</span>]</span> <a title="Permalink to this symbol" href="#Unique.this" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L121">#</a></dt>
<dd class="ddef">
<div class="summary">Constructor that takes an rvalue.
        It will ensure uniqueness, as long as the rvalue
        isn't just a view on an lvalue (e.g., a cast)
        Typical usage:
        <pre class="d_code">
<span class="i">Unique</span>!(<span class="i">Foo</span>) <span class="i">f</span> = <span class="k">new</span> <span class="i">Foo</span>;
</pre></div></dd>
<dt class="decl"><a class="symbol _ctor" name="Unique.this:2" href="./htmlsrc/std.typecons.html#L131" kind="ctor" beg="131" end="137">this</a><span class="params">(ref RefT <em>p</em>)</span>; <span class="attrs">[<span class="prot">public</span>]</span> <a title="Permalink to this symbol" href="#Unique.this:2" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L131">#</a></dt>
<dd class="ddef">
<div class="summary">Constructor that takes an lvalue. It nulls its source.
        The nulling will ensure uniqueness as long as there
        are no previous aliases to the source.</div></dd>
<dt class="decl"><a class="symbol _dtor" name="Unique.~this" href="./htmlsrc/std.typecons.html#L164" kind="dtor" beg="164" end="169">~this</a>(); <span class="attrs">[<span class="prot">public</span>]</span> <a title="Permalink to this symbol" href="#Unique.~this" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L164">#</a></dt>
<dd class="ddef"></dd>
<dt class="decl">bool <a class="symbol _function" name="Unique.isEmpty" href="./htmlsrc/std.typecons.html#L170" kind="function" beg="170" end="173">isEmpty</a><span class="params">()</span>; <span class="attrs">[<span class="prot">public</span>, <span class="stc">const</span>]</span> <a title="Permalink to this symbol" href="#Unique.isEmpty" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L170">#</a></dt>
<dd class="ddef"></dd>
<dt class="decl">Unique <a class="symbol _function" name="Unique.release" href="./htmlsrc/std.typecons.html#L175" kind="function" beg="175" end="182">release</a><span class="params">()</span>; <span class="attrs">[<span class="prot">public</span>]</span> <a title="Permalink to this symbol" href="#Unique.release" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L175">#</a></dt>
<dd class="ddef">
<div class="summary">Returns a unique rvalue. Nullifies the current contents</div></dd>
<dt class="decl">RefT <a class="symbol _function" name="Unique.opDot" href="./htmlsrc/std.typecons.html#L184" kind="function" beg="184" end="184">opDot</a><span class="params">()</span>; <span class="attrs">[<span class="prot">public</span>]</span> <a title="Permalink to this symbol" href="#Unique.opDot" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L184">#</a></dt>
<dd class="ddef">
<div class="summary">Forwards member access to contents</div></dd>
<dt class="decl">RefT <a class="symbol _variable" name="Unique._p" href="./htmlsrc/std.typecons.html#L194" kind="variable" beg="194" end="194">_p</a>; <span class="attrs">[<span class="prot">private</span>]</span> <a title="Permalink to this symbol" href="#Unique._p" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L194">#</a></dt>
<dd class="ddef"></dd></dl></dd>
<dt class="decl">template <a class="symbol _template" name="tupleImpl" href="./htmlsrc/std.typecons.html#L250" kind="template" beg="250" end="272">tupleImpl</a><span class="tparams">(uint index, T...)</span>; <span class="attrs">[<span class="prot">private</span>]</span> <a title="Permalink to this symbol" href="#tupleImpl" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L250">#</a></dt>
<dd class="ddef">
<dl>
<dt class="decl">string <a class="symbol _variable" name="tupleImpl.result" href="./htmlsrc/std.typecons.html#L254" kind="variable" beg="254" end="254">result</a>; <span class="attrs">[<span class="stc">manifest</span>]</span> <a title="Permalink to this symbol" href="#tupleImpl.result" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L254">#</a></dt>
<dd class="ddef"></dd></dl></dd>
<dt class="decl">struct <a class="symbol _struct" name="Tuple" href="./htmlsrc/std.typecons.html#L328" kind="struct" beg="328" end="348">Tuple</a><span class="tparams">(T...)</span>; <a title="Permalink to this symbol" href="#Tuple" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L328">#</a></dt>
<dd class="ddef">
<div class="summary">Tuple of values, for example <span class="d_param">Tuple!(int, string)</span> is a
record that stores an <span class="d_param">int</span> and a <span class="d_param">
string</span>. <span class="d_param">Tuple</span> can be used to bundle values together,
notably when returning multiple values from a function. If <span class="d_param">
obj</span> is a tuple, the individual members are accessible with the syntax
<span class="d_param">obj.field!(0)</span> for the first field, <span class="d_param">obj.field!(1)</span>
for the second, and so on. A shortcut notation is <span class="d_param">
obj.</span>&#95;<span class="d_param">0</span>, <span class="d_param">obj.</span>&#95;<span class="d_param">1</span> etc.</div>
The choice of zero-based indexing instead of one-base indexing was
motivated by the ability to use value tuples with various compile-time
loop constructs (e.g. type tuple iteration), all of which use
zero-based indexing.
<p class="sec_header">Example:</p><pre class="d_code">
<span class="i">Tuple</span>!(<span class="k">int</span>, <span class="k">int</span>) <span class="i">point</span>;
<span class="lc">// assign coordinates</span>
<span class="i">point</span>.<span class="i">_0</span> = <span class="n">5</span>;
<span class="i">point</span>.<span class="i">field</span>!(<span class="n">1</span>) = <span class="n">6</span>;
<span class="lc">// read coordinates</span>
<span class="k">auto</span> <span class="i">x</span> = <span class="i">point</span>.<span class="i">field</span>!(<span class="n">0</span>);
<span class="k">auto</span> <span class="i">y</span> = <span class="i">point</span>.<span class="i">_1</span>;
</pre>
<p class="bl"/>
Tuple members can be named. It is legal to mix named and unnamed
members. The method above is still applicable to all fields.
<p class="sec_header">Example:</p><pre class="d_code">
<span class="k">alias</span> <span class="i">Tuple</span>!(<span class="k">int</span>, <span class="sl">"index"</span>, <span class="i">string</span>, <span class="sl">"value"</span>) <span class="i">Entry</span>;
<span class="i">Entry</span> <span class="i">e</span>;
<span class="i">e</span>.<span class="i">index</span> = <span class="n">4</span>;
<span class="i">e</span>.<span class="i">value</span> = <span class="sl">"Hello"</span>;
<span class="k">assert</span>(<span class="i">e</span>.<span class="i">_1</span> == <span class="sl">"Hello"</span>);
<span class="k">assert</span>(<span class="i">e</span>.<span class="i">field</span>!(<span class="n">0</span>) == <span class="n">4</span>);
</pre>
<p class="bl"/>
Tuples with named fields are distinct types from tuples with unnamed
fields, i.e. each naming imparts a separate type for the tuple. Two
tuple differing in naming only are still distinct, even though they
might have the same structure.
<p class="sec_header">Example:</p><pre class="d_code">
<span class="i">Tuple</span>!(<span class="k">int</span>, <span class="sl">"x"</span>, <span class="k">int</span>, <span class="sl">"y"</span>) <span class="i">point1</span>;
<span class="i">Tuple</span>!(<span class="k">int</span>, <span class="k">int</span>) <span class="i">point2</span>;
<span class="k">assert</span>(!<span class="k">is</span>(<span class="k">typeof</span>(<span class="i">point1</span>) == <span class="k">typeof</span>(<span class="i">point2</span>))); <span class="lc">// passes</span>
</pre>
<dl>
<dt class="decl">string <a class="symbol _function" name="Tuple.toString" href="./htmlsrc/std.typecons.html#L331" kind="function" beg="331" end="347">toString</a><span class="params">()</span>; <a title="Permalink to this symbol" href="#Tuple.toString" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L331">#</a></dt>
<dd class="ddef"></dd></dl></dd>
<dt class="decl"><a class="symbol _unittest" name="unittest" href="./htmlsrc/std.typecons.html#L350" kind="unittest" beg="350" end="358">unittest</a>; <a title="Permalink to this symbol" href="#unittest" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L350">#</a></dt>
<dd class="ddef"></dd>
<dt class="decl">Tuple!(T) <a class="symbol _function" name="tuple" href="./htmlsrc/std.typecons.html#L373" kind="function" beg="373" end="383">tuple</a><span class="tparams">(T...)</span><span class="params">(T <em>args</em>)</span>; <a title="Permalink to this symbol" href="#tuple" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L373">#</a></dt>
<dd class="ddef">
<div class="summary">Returns a <span class="d_psymbol"><i>Tuple</i></span> object instantiated and initialized according to
the arguments.</div>
<p class="sec_header">Example:</p><pre class="d_code">
<span class="k">auto</span> <span class="i">value</span> = <span class="i">tuple</span>(<span class="n">5</span>, <span class="n">6.7</span>, <span class="sl">"hello"</span>);
<span class="k">assert</span>(<span class="i">value</span>.<span class="i">_0</span> == <span class="n">5</span>);
<span class="k">assert</span>(<span class="i">value</span>.<span class="i">_1</span> == <span class="n">6.7</span>);
<span class="k">assert</span>(<span class="i">value</span>.<span class="i">_2</span> == <span class="sl">"hello"</span>);
</pre></dd>
<dt class="decl">template <a class="symbol _template" name="enumValuesImpl" href="./htmlsrc/std.typecons.html#L385" kind="template" beg="385" end="413">enumValuesImpl</a><span class="tparams">(string name, BaseType, long index, T...)</span>; <span class="attrs">[<span class="prot">private</span>]</span> <a title="Permalink to this symbol" href="#enumValuesImpl" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L385">#</a></dt>
<dd class="ddef">
<dl>
<dt class="decl">string <a class="symbol _variable" name="enumValuesImpl.enumValuesImpl" href="./htmlsrc/std.typecons.html#L389" kind="variable" beg="389" end="390">enumValuesImpl</a>; <span class="attrs">[<span class="stc">manifest</span>]</span> <a title="Permalink to this symbol" href="#enumValuesImpl.enumValuesImpl" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L389">#</a></dt>
<dd class="ddef"></dd></dl></dd>
<dt class="decl">template <a class="symbol _template" name="enumParserImpl" href="./htmlsrc/std.typecons.html#L415" kind="template" beg="415" end="433">enumParserImpl</a><span class="tparams">(string name, bool first, T...)</span>; <span class="attrs">[<span class="prot">private</span>]</span> <a title="Permalink to this symbol" href="#enumParserImpl" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L415">#</a></dt>
<dd class="ddef">
<dl>
<dt class="decl">string <a class="symbol _variable" name="enumParserImpl.enumParserImpl" href="./htmlsrc/std.typecons.html#L419" kind="variable" beg="419" end="422">enumParserImpl</a>; <span class="attrs">[<span class="stc">manifest</span>]</span> <a title="Permalink to this symbol" href="#enumParserImpl.enumParserImpl" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L419">#</a></dt>
<dd class="ddef"></dd></dl></dd>
<dt class="decl">template <a class="symbol _template" name="enumPrinterImpl" href="./htmlsrc/std.typecons.html#L435" kind="template" beg="435" end="451">enumPrinterImpl</a><span class="tparams">(string name, bool first, T...)</span>; <span class="attrs">[<span class="prot">private</span>]</span> <a title="Permalink to this symbol" href="#enumPrinterImpl" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L435">#</a></dt>
<dd class="ddef">
<dl>
<dt class="decl">string <a class="symbol _variable" name="enumPrinterImpl.enumPrinterImpl" href="./htmlsrc/std.typecons.html#L439" kind="variable" beg="439" end="440">enumPrinterImpl</a>; <span class="attrs">[<span class="stc">manifest</span>]</span> <a title="Permalink to this symbol" href="#enumPrinterImpl.enumPrinterImpl" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L439">#</a></dt>
<dd class="ddef"></dd></dl></dd>
<dt class="decl">template <a class="symbol _template" name="ValueTuple" href="./htmlsrc/std.typecons.html#L453" kind="template" beg="453" end="456">ValueTuple</a><span class="tparams">(T...)</span>; <span class="attrs">[<span class="prot">private</span>]</span> <a title="Permalink to this symbol" href="#ValueTuple" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L453">#</a></dt>
<dd class="ddef">
<dl>
<dt class="decl">alias T <a class="symbol _alias" name="ValueTuple.ValueTuple" href="./htmlsrc/std.typecons.html#L455" kind="alias" beg="455" end="455">ValueTuple</a>; <a title="Permalink to this symbol" href="#ValueTuple.ValueTuple" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L455">#</a></dt>
<dd class="ddef"></dd></dl></dd>
<dt class="decl">template <a class="symbol _template" name="StringsOnly" href="./htmlsrc/std.typecons.html#L458" kind="template" beg="458" end="470">StringsOnly</a><span class="tparams">(T...)</span>; <span class="attrs">[<span class="prot">private</span>]</span> <a title="Permalink to this symbol" href="#StringsOnly" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L458">#</a></dt>
<dd class="ddef">
<dl>
<dt class="decl">alias ValueTuple!(T[0]) <a class="symbol _alias" name="StringsOnly.StringsOnly" href="./htmlsrc/std.typecons.html#L462" kind="alias" beg="462" end="462">StringsOnly</a>; <a title="Permalink to this symbol" href="#StringsOnly.StringsOnly" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L462">#</a></dt>
<dd class="ddef"></dd></dl></dd>
<dt class="decl">template <a class="symbol _template" name="defineEnum" href="./htmlsrc/std.typecons.html#L505" kind="template" beg="505" end="514">defineEnum</a><span class="tparams">(string name, T...)</span>; <a title="Permalink to this symbol" href="#defineEnum" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L505">#</a></dt>
<dd class="ddef">
<div class="summary">Defines truly named enumerated values with parsing and stringizing
primitives.</div>
<p class="sec_header">Example:</p><pre class="d_code">
<span class="k">mixin</span>(<span class="i">defineEnum</span>!(<span class="sl">"Abc"</span>, <span class="sl">"A"</span>, <span class="sl">"B"</span>, <span class="n">5</span>, <span class="sl">"C"</span>));
</pre>
<p class="bl"/>
is equivalent to the following code:
<p class="bl"/>
<pre class="d_code">
<span class="k">enum</span> <span class="i">Abc</span> { <span class="i">A</span>, <span class="i">B</span> = <span class="n">5</span>, <span class="i">C</span> }
<span class="i">string</span> <span class="i">enumToString</span>(<span class="i">Abc</span> <span class="i">v</span>) { ... }
<span class="i">Abc</span> <span class="i">enumFromString</span>(<span class="i">string</span> <span class="i">s</span>) { ... }
</pre>
<p class="bl"/>
The <span class="d_param">enumToString</span> function generates the unqualified names
of the enumerated values, i.e. "A", "B", and "C". The <span class="d_param">
enumFromString</span> function expects one of "A", "B", and "C", and throws
an exception in any other case.
<p class="bl"/>
A base type can be specified for the enumeration like this:
<p class="bl"/>
<pre class="d_code">
<span class="k">mixin</span>(<span class="i">defineEnum</span>!(<span class="sl">"Abc"</span>, <span class="k">ubyte</span>, <span class="sl">"A"</span>, <span class="sl">"B"</span>, <span class="sl">"C"</span>, <span class="n">255</span>));
</pre>
<p class="bl"/>
In this case the generated <span class="d_param">enum</span> will have a <span class="d_param">
ubyte</span> representation.
<dl>
<dt class="decl">string <a class="symbol _variable" name="defineEnum.defineEnum" href="./htmlsrc/std.typecons.html#L508" kind="variable" beg="508" end="511">defineEnum</a>; <span class="attrs">[<span class="stc">manifest</span>]</span> <a title="Permalink to this symbol" href="#defineEnum.defineEnum" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L508">#</a></dt>
<dd class="ddef"></dd></dl></dd>
<dt class="decl"><a class="symbol _unittest" name="unittest:2" href="./htmlsrc/std.typecons.html#L516" kind="unittest" beg="516" end="525">unittest</a>; <a title="Permalink to this symbol" href="#unittest:2" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L516">#</a></dt>
<dd class="ddef"></dd>
<dt class="decl">template <a class="symbol _template" name="Rebindable" href="./htmlsrc/std.typecons.html#L561" kind="template" beg="561" end="592">Rebindable</a><span class="tparams">(T : Object)</span>; <a title="Permalink to this symbol" href="#Rebindable" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L561">#</a></dt>
<dd class="ddef">
<div class="summary"><span class="d_psymbol"><i>Rebindable!(T)</i></span> is a simple, efficient wrapper that behaves just
like an object of type <span class="d_psymbol"><i>T</i></span>, except that you can reassign it to
refer to another object. For completeness, <span class="d_psymbol"><i>Rebindable!(T)</i></span> aliases
itself away to <span class="d_psymbol"><i>T</i></span> if <span class="d_psymbol"><i>T</i></span> is a non-const object type. However,
<span class="d_psymbol"><i>Rebindable!(T)</i></span> does not compile if <span class="d_psymbol"><i>T</i></span> is a non-class type.</div>
Regular <span class="d_psymbol"><i>const</i></span> object references cannot be reassigned:
<p class="bl"/>
<pre class="d_code">
<span class="k">class</span> <span class="i">Widget</span> { <span class="k">int</span> <span class="i">x</span>; <span class="k">int</span> <span class="i">y</span>() <span class="k">const</span> { <span class="k">return</span> <span class="i">a</span>; } }
<span class="k">const</span> <span class="i">a</span> = <span class="k">new</span> <span class="i">Widget</span>;
<span class="i">a</span>.<span class="i">y</span>();          <span class="lc">// fine</span>
<span class="i">a</span>.<span class="i">x</span> = <span class="n">5</span>;        <span class="lc">// error! can't modify const a</span>
<span class="i">a</span> = <span class="k">new</span> <span class="i">Widget</span>; <span class="lc">// error! can't modify const a</span>
</pre>
<p class="bl"/>
However, <span class="d_psymbol"><i>Rebindable!(Widget)</i></span> does allow reassignment, while
otherwise behaving exactly like a <span class="d_psymbol"><i>const Widget</i></span>:
<p class="bl"/>
<pre class="d_code">
<span class="k">auto</span> <span class="i">a</span> = <span class="i">Rebindable</span>!(<span class="k">const</span> <span class="i">Widget</span>)(<span class="k">new</span> <span class="i">Widget</span>);
<span class="i">a</span>.<span class="i">y</span>();          <span class="lc">// fine</span>
<span class="i">a</span>.<span class="i">x</span> = <span class="n">5</span>;        <span class="lc">// error! can't modify const a</span>
<span class="i">a</span> = <span class="k">new</span> <span class="i">Widget</span>; <span class="lc">// fine</span>
</pre>
<p class="bl"/>
You may want to use <span class="d_psymbol"><i>Rebindable</i></span> when you want to have mutable
storage referring to <span class="d_psymbol"><i>const</i></span> objects, for example an array of
references that must be sorted in place. <span class="d_psymbol"><i>Rebindable</i></span> does not
break the soundness of D's type system and does not incur any of the
risks usually associated with <span class="d_psymbol"><i>cast</i></span>.
<dl>
<dt class="decl">alias T <a class="symbol _alias" name="Rebindable.Rebindable" href="./htmlsrc/std.typecons.html#L565" kind="alias" beg="565" end="565">Rebindable</a>; <a title="Permalink to this symbol" href="#Rebindable.Rebindable" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L565">#</a></dt>
<dd class="ddef"></dd></dl></dd>
<dt class="decl"><a class="symbol _unittest" name="unittest:3" href="./htmlsrc/std.typecons.html#L594" kind="unittest" beg="594" end="616">unittest</a>; <a title="Permalink to this symbol" href="#unittest:3" class="symlink">¶</a><a title="Go to the HTML source file" class="srclink" href="./htmlsrc/std.typecons.html#L594">#</a></dt>
<dd class="ddef"></dd></dl>
</div>
<div id="footer">
  <p>Copyright © 1999-2008 by Digital Mars ®, All Rights Reserved.</p>
  <p>Page generated by <a href="http://code.google.com/p/dil">dil</a> on Sun Dec 28 04:26:46 2008. Rendered by <a href="http://code.google.com/p/dil/wiki/Kandil">kandil</a>.</p>
</div>
</body>
</html>